package com.exam.dao;

import com.exam.domain.entity.Exam;
import com.exam.domain.entity.Question;
import com.exam.domain.entity.User;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class QuestionDao {

    public Question getQuestionById(Integer questionId) {
        String sql = "SELECT * FROM questions WHERE question_id = ?";
        // 执行查询操作，返回结果集
        // 执行查询操作，返回 ResultSet
        try (Connection connection = DBUtil.getConnection();
             ResultSet rs = DBUtil.query(connection, sql, questionId)){
            if (rs.next()) {
                // 结果集中有数据，映射到 Question 对象
                Question question = new Question();
                question.setQuestionText(rs.getString("question_text"));
                question.setCorrectOption(rs.getString("correct_option"));
                question.setOptionA(rs.getString("option_a"));
                question.setOptionB(rs.getString("option_b"));
                question.setOptionC(rs.getString("option_c"));
                question.setOptionD(rs.getString("option_d"));
                question.setQuestionId(rs.getInt("question_id"));
                question.setCreatedBy(rs.getInt("created_by"));
                // 返回查询到的用户
                return question;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询问题失败", e);
        }
        // 如果没有找到用户，返回 null
        return null;
    }

    public Integer updateQuestion(Integer questionId, String questionText, String correctOption, String optionA, String optionB, String optionC, String optionD) {
        // SQL 更新语句
        String sql = "UPDATE questions set question_text=?, correct_option=?, option_a=?, option_b=?, option_c=?, option_d=? WHERE question_id = ?";

        try  {
            // 获取数据库连接
            Connection connection = DBUtil.getConnection();

            int result = DBUtil.update(connection, sql, questionText, correctOption, optionA, optionB, optionC, optionD, questionId);

            if (result > 0) {
                return 1; // 成功更新数据，返回 1
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新试题失败", e);
        }

        // 更新失败，返回 0
        return 0;
    }

    public Integer deleteQuestionById(Integer questionId) {
        String sql = "DELETE from questions WHERE question_id = ?";

        try  {
            // 获取数据库连接
            Connection connection = DBUtil.getConnection();

            int result = DBUtil.update(connection, sql, questionId);

            if (result > 0) {
                return 1; // 成功更新数据，返回 1
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除试题失败", e);
        }

        // 插入失败，返回 0
        return 0;

    }

    public List<Question> getQuestionListByExamId(Integer examId) {
        String sql1 = "SELECT * FROM exam_questions WHERE exam_id = ?";
        // 执行查询操作，返回结果集
        // 执行查询操作，返回 ResultSet
        List<Question> questions = new ArrayList<>();
        try {
            Connection connection = DBUtil.getConnection();
            ResultSet rs = DBUtil.query(connection, sql1, examId);
            while (rs.next()) {
                // created_by, question_text, correct_option, option_a, option_b, option_c, option_d
                // 结果集中有数据，映射到 User 对象
                Integer questionId = rs.getInt("question_id");
                // 通过问题id，查询问题表
                String sql2= "SELECT * FROM questions WHERE question_id = ?";
                // 使用新的连接进行查询
                try (Connection questionConnection = DBUtil.getConnection();
                     ResultSet questionRs = DBUtil.query(questionConnection, sql2, questionId)) {

                    if (questionRs.next()) {
                        Question question = new Question();
                        question.setQuestionId(questionRs.getInt("question_id"));
                        question.setQuestionText(questionRs.getString("question_text"));
                        question.setCorrectOption(questionRs.getString("correct_option"));
                        question.setOptionA(questionRs.getString("option_a"));
                        question.setOptionB(questionRs.getString("option_b"));
                        question.setOptionC(questionRs.getString("option_c"));
                        question.setOptionD(questionRs.getString("option_d"));
                        question.setCreatedBy(questionRs.getInt("created_by"));
                        questions.add(question);
                    }
                }
            }
            return questions;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询试题失败", e);
        }
    }
}
